package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.Collect;
import com.wm.bean.OrderMenu;
import com.wm.comm.Constant;
import com.wm.dao.CollectDAO;

/**
 * @author yangbo
 *
 * 2012-12-26
 */
public class CollectHibernateDAO extends HibernateDaoSupport implements CollectDAO {

	private static final String LOAD_BY_ID = "from Collect where id = ?";
	private static final String LOAD_BY_USERID_OBJTYPE = "from Collect where userId = ? and objType = ? order by id desc";
	private static final String LOAD_BY_USERID_OBJID_OBJTYPE = "from Collect where userId = ? and objId = ? and objType = ?";
	
	/**
	 * 添加
	 * @param c
	 * @return
	 */
	public Collect saveCollect(Collect c){
		this.getHibernateTemplate().save(c);
		return c;
	}
	
	/**
	 * 删除
	 * @param c
	 */
	public void removeCollect(Collect c){
		this.getHibernateTemplate().delete(c);
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 */
	public Collect findCollectById(long id){
		Object[] obj={new Long(id)};
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,obj);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Collect) l.get(0);
		}
	}
	
	/**
	 * 根据用户ID查询
	 * @param userId
	 * @return
	 */
	public List findCollectByUserIdObjType(long userId,int objType){
		return this.getHibernateTemplate().find(LOAD_BY_USERID_OBJTYPE,new Long(userId),new Integer(objType));
	}
	
	public Collect findCollectByUserIdObjIdObjType(long userId,long objId,int objType){
		Object[] obj={new Long(userId),new Long(objId),new Integer(objType)};
		List l = this.getHibernateTemplate().find(LOAD_BY_USERID_OBJID_OBJTYPE,obj);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Collect) l.get(0);
		}
	}
	
	public List findCollectByUserIdObjTypeFirstResultMaxResults(long userId,int objType,String orderby,int ascOrDesc,final int firstResult,final int maxResults){
		StringBuffer sb = new StringBuffer();
		sb.append("from Collect where 1=1");
		sb.append(" and userId=");
		sb.append(userId);
		sb.append(" and objType=");
		sb.append(objType);
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				List list = query.list();
				return list;
			}
		});
	}
}
